package beer.xz.beer.device

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.addCallback
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import beer.xz.beer.device.data.LoadConfig
import beer.xz.beer.device.ui.cover.CoverScreen
import beer.xz.beer.device.ui.loading.LoadingScreen
import beer.xz.beer.device.ui.register.LoadFailScreen
import beer.xz.beer.device.ui.selection.SelectionScreen
import beer.xz.beer.device.ui.test.ProtocolTestScreen
import beer.xz.beer.device.ui.test.TestScreen
import beer.xz.beer.device.ui.theme.DBeerDeviceTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            DBeerDeviceTheme {
                val navController = rememberNavController()

                NavHost(
                    navController = navController,
                    startDestination = "test",
                    modifier = Modifier.fillMaxSize()
                ) {
                    composable("loading") {
                        var loadingProgress by remember { mutableFloatStateOf(0f) }
                        var loadingText by remember { mutableStateOf("") }

                        LoadConfig({ text, progress ->
                            loadingProgress = progress
                            loadingText = text
                        }, onLoaded = { navController.navigate("cover") }) {
                            navController.navigate("register")
                        }
                        LoadingScreen(
                            text = loadingText, progress = loadingProgress
                        )
                    }
                    composable("cover") {
                        CoverScreen { navController.navigate("selection") }
                    }
                    composable("selection") {
                        SelectionScreen { navController.navigate("cover") }
                    }
                    composable("register") {
                        LoadFailScreen(navController)
                    }
                    composable("test") {
                        TestScreen()
                    }
                }
            }
        }
        this.onBackPressedDispatcher.addCallback { }
    }

//    @Deprecated("This method has been deprecated in favor of using the\n      {@link OnBackPressedDispatcher} via {@link #getOnBackPressedDispatcher()}.\n      The OnBackPressedDispatcher controls how back button events are dispatched\n      to one or more {@link OnBackPressedCallback} objects.")
//    @SuppressLint("MissingSuperCall")
//    override fun onBackPressed() {
//        // 不调用 super.onBackPressed() 以禁用后退按钮
//    }

}
